﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Linq;
using System.Collections.ObjectModel;
using WP_Customer_ShopMob.ViewModels;
using System.Net;
using Newtonsoft.Json;
using WP_Customer_ShopMob.Model;
using System.Runtime.Serialization;

namespace WP_Customer_ShopMob.ViewModels
{
    [DataContract]
    public class CategorieViewModel : BaseViewModel
    {
        private Model.IRepository repo = Data.GetModel;
        private long id;
        [DataMember]
        public long Id
        {
            get { return id; }
            set { id = value; }
        }

        private string nom;
        [DataMember]
        public string Nom
        {
            get { return nom; }
            set { nom = value; }
        }

        private string details;
        [DataMember]
        public string Details
        {
            get { return details; }
            set { details = value; }
        }

        private long idMere;
        [DataMember]
        public long IdMere
        {
            get { return idMere; }
            set { idMere = value; }
        }

        private ObservableCollection<ArticleViewModel> listArticles = new ObservableCollection<ArticleViewModel>();
        [DataMember]
        public ObservableCollection<ArticleViewModel> ListArticles
        {
            get { return listArticles; }
            set { listArticles = value; }
        }

        private ObservableCollection<CategorieViewModel> listSousCategories = new ObservableCollection<CategorieViewModel>();
        [DataMember]
        public ObservableCollection<CategorieViewModel> ListSousCategories
        {
            get { return listSousCategories; }
            set { listSousCategories = value; }
        }

        private bool isDataLoaded = false;
        [DataMember]
        public bool IsDataLoaded
        {
            get { return isDataLoaded; }
            set { isDataLoaded = value; }
        }

        public CategorieViewModel() { }
        public CategorieViewModel(long id, string nom, string detail, long idPere)
        {
            this.Details = detail;
            this.Id = id;
            this.Nom = nom;
            this.IdMere = idPere;
        }

        public void LoadData(System.Windows.Navigation.NavigationService nav)
        {
            repo.GetCategoriesFille(Id, delegate(string resu, Exception e)
            {
                var categs = JsonConvert.DeserializeObject<List<DBO_ShopMob.Categorie>>(resu);
                if (categs.Count != 0)
                {
                    var categories = Helper.IsolatedStorage.Load<ObservableCollection<CategorieViewModel>>("categories");
                    if (categories == null) categories = new ObservableCollection<CategorieViewModel>();
                    foreach (var categ in categs)
                    {
                        CategorieViewModel cvm = new CategorieViewModel() { Details = categ.details, Id = categ.id, IdMere = categ.idMere, Nom = categ.nom, IsDataLoaded = false };
                        ListSousCategories.Add(cvm);
                        if (categories.SingleOrDefault(x => x.Id == cvm.Id) == null)
                        {
                            categories.Add(cvm);
                        }
                    }
                    Helper.IsolatedStorage.Save<ObservableCollection<CategorieViewModel>>(categories, "categories");
                    nav.Navigate(new Uri("/Views/sousCategoriesPage.xaml?id=" + this.Id, UriKind.Relative));
                }
                else
                {
                    repo.GetArticlesByCateg(Id, delegate(string resultat, Exception ex)
                    {
                        var arts = JsonConvert.DeserializeObject<List<DBO_ShopMob.Article>>(resultat);
                        if (arts.Count != 0)
                        {
                            var articles = Helper.IsolatedStorage.Load<ObservableCollection<ArticleViewModel>>("articles");
                            if (articles == null) articles = new ObservableCollection<ArticleViewModel>();
                            foreach (var art in arts)
                            {
                                ArticleViewModel avm = new ArticleViewModel { Details = art.details, IdArticle = art.id, Nom = art.nom, Prix = art.prix, IdCategorie = art.categorie };
                                ListArticles.Add(avm);
                                if (articles.SingleOrDefault(x => x.IdArticle == avm.IdArticle) == null)
                                {
                                    articles.Add(avm);
                                }
                            }
                            Helper.IsolatedStorage.Save<ObservableCollection<ArticleViewModel>>(articles, "articles");
                            nav.Navigate(new Uri("/Views/CategoriesPage.xaml?id=" + this.Id, UriKind.Relative));
                        }
                        else
                            MessageBox.Show("Aucun contenu disponible");
                    });
                }
                IsDataLoaded = true;
            });
            //if (Id == 1)
            //{
            //    List<DBO_ShopMob.Categorie> list = new List<DBO_ShopMob.Categorie>();
            //    list.Add(new DBO_ShopMob.Categorie() { details = "sport", id = 4, idMere = 1, nom = "Tennis" });
            //    list.Add(new DBO_ShopMob.Categorie() { details = "sport", id = 2, idMere = 1, nom = "Football" });
            //    var categs = Helper.IsolatedStorage.Load<ObservableCollection<CategorieViewModel>>("categories");
            //    foreach (var categ in list)
            //    {
            //        CategorieViewModel cvm = new CategorieViewModel() { Details = categ.details, Id = categ.id, IdMere = categ.idMere, Nom = categ.nom, IsDataLoaded = false };
            //        ListSousCategories.Add(cvm);
            //        if (categs.SingleOrDefault(x => x.Id == cvm.Id) == null)
            //        {
            //            categs.Add(cvm);
            //        }
            //    }
            //    categs.SingleOrDefault(x => x.Id == this.Id).IsDataLoaded = true;
            //    Helper.IsolatedStorage.Save<ObservableCollection<CategorieViewModel>>(categs, "categories");
            //}
            //IsDataLoaded = true;
        }
    }
}
